<template>
	<view class="slider-verify-box" v-if="isShow">
		<view class="verify-box">
			<view class="slider-title">图形验证</view>
			<view class="slide-content">
				<view class="slide-tips">拖动下方滑块完成拼图</view>
				<view class="slider-pintu">
					<image id="pintuImg" class="pintu" :src="imgSrc" mode="aspectFill"></image>

					<view class="pintukuai" :style="{ top: top + 'px', left: oldx + 'px' }"><image class="image" :src="imgSrc" :style="{ top: '-' + top + 'px', left: '-' + left + 'px' }" mode="aspectFill"></image></view>

					<view class="yinying" :style="{ top: top + 'px', left: left + 'px' }"></view>
				</view>

				<view class="slider-movearea" @touchend="endTouchMove">
					<movable-area :animation="true"><movable-view :x="x" direction="horizontal" @change="startMove"></movable-view></movable-area>

					<view class="huadao">拖动左边滑块完成上方拼图</view>
				</view>
			</view>

			<view class="slider-btn-group">
				<view class="slider-btn" @click="closeSlider">关闭</view>
				<view class="slider-btn slide-btn-refresh" @click="refreshVerify">刷新</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'slider-verify',
	props: {
		isShow: true
	},
	data() {
		return {
			x: 0, //初始距离
			oldx: 0, //移动的距离
			img: '1', //显示哪张图片
			// imgSrc: 'https://api.isoyu.com/beibei_images.php',
			imgSrc: 'https://www.dmoe.cc/random.php?mom=',
			left: 0, //随机拼图的最终X轴距离
			top: 0 //拼图的top距离
		};
	},
	watch: {
		// 每次打开重新刷新拼图
		isShow(newValue, oldValue) {
			if (newValue) {
				this.refreshVerify(); //刷新
			}
		}
	},
	mounted() {
		this.refreshVerify();
	},
	methods: {
		//刷新验证
		refreshVerify() {
			var gl = Math.random().toFixed(2);
			this.left = uni.upx2px(560) * gl > uni.upx2px(280) ? uni.upx2px(280) : uni.upx2px(560) * gl + uni.upx2px(150); //生成随机X轴最终距离
			this.top = uni.upx2px(190) * gl; //生成随机Y轴初始距离

			if (gl <= 0.2) {
				this.img = 1;
				this.imgSrc = this.imgSrc + '&mom=' + this.img;
			}
			if (gl > 0.2 && gl <= 0.4) {
				this.img = 2;
				this.imgSrc = this.imgSrc + '&mom=' + this.img;
			}
			if (gl > 0.4 && gl <= 0.6) {
				this.img = 3;
				this.imgSrc = this.imgSrc + '&mom=' + this.img;
			}
			if (gl > 0.6 && gl <= 0.8) {
				this.img = 4;
				this.imgSrc = this.imgSrc + '&mom=' + this.img;
			}
			if (gl > 0.8 && gl <= 1) {
				this.img = 5;
				this.imgSrc = this.imgSrc + '&mom=' + this.img;
			}

			this.resetMove(); //重置阴影位置
		},

		/* 滑动中 */
		startMove(e) {
			this.oldx = e.detail.x;
		},

		/* 滑动结束 */
		endTouchMove() {
			if (Math.abs(this.oldx - this.left) <= 5) {
				this.$emit('touchSliderResult', true);
			} else {
				this.refreshVerify();
			}
		},

		/* 重置阴影位置 */
		resetMove() {
			this.x = 1;
			this.oldx = 1;
			setTimeout(() => {
				this.x = 0;
				this.oldx = 0;
			}, 300);
		},

		// 关闭
		closeSlider() {
			this.$emit('touchSliderResult', false);
		}
	}
};
</script>

<style lang="less">
.slider-verify-box {
	position: fixed;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
	z-index: 999;
}
.verify-box {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%, -50%);
	// width: 85%;
	background-color: #fff;
	border-radius: 12rpx;
	box-shadow: 0 0 5rpx rgba(0, 0, 0);

	.slider-title {
		padding: 20rpx 0;
		font-size: 18px;
		color: rgba(2, 20, 33, 0.85);
		text-align: center;
		border-bottom: 2rpx solid rgba(2, 20, 33, 0.15);
	}

	.slide-content {
		margin: 0 auto;
		padding: 0 30rpx;
		width: 560rpx;

		.slide-tips {
			padding: 20rpx 0;
			font-size: 14px;
			color: rgba(2, 20, 33, 0.45);
		}

		.slider-pintu {
			position: relative;
			width: 100%;
			border-radius: 12rpx;
			overflow: hidden;

			.pintu {
				display: block;
				margin: 0 auto;
				width: 500rpx;
				height: 314rpx;
				object-fit: contain;
			}

			.pintukuai {
				position: absolute;
				top: 0;
				left: 0;
				z-index: 100;
				width: 120rpx;
				height: 120rpx;
				box-shadow: 0 0 5rpx rgba(0, 0, 0, 0.3);
				overflow: hidden;

				.image {
					display: block;
					position: absolute;
					top: 0;
					left: 0;
					width: 500rpx;
					height: 314rpx;
					object-fit: contain;
				}
			}
		}

		.yinying {
			position: absolute;
			width: 120rpx;
			height: 120rpx;
			background-color: rgba(0, 0, 0, 0.5);
		}
	}
}

.slider-movearea {
	position: relative;
	margin: 20rpx auto;
	width: 100%;
	height: 80rpx;

	movable-area {
		height: 80rpx;
		width: 100%;

		movable-view {
			position: relative;
			z-index: 100;
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			background: #007cff url('~@/static/icon-button-normal.png') no-repeat center center;
			background-size: auto 30rpx;
		}
	}
}

.huadao {
	position: absolute;
	top: 7rpx;
	left: 0;
	z-index: 99;
	width: 100%;
	height: 66rpx;
	font-size: 14px;
	color: #999;
	line-height: 66rpx;
	text-align: center;
	border-radius: 12rpx;
	box-sizing: border-box;
	box-shadow: inset 0 0 5rpx #ccc;
	background: #eee;
}

.slider-btn-group {
	display: flex;
	justify-content: center;
	align-items: center;
	width: 100%;
	border-top: 2rpx solid rgba(2, 20, 33, 0.15);

	.slider-btn {
		flex: 1;
		height: 100rpx;
		font-size: 18px;
		color: rgba(2, 20, 33, 0.85);
		line-height: 100rpx;
		text-align: center;

		&:active {
			opacity: 0.8;
		}
	}

	.slide-btn-refresh {
		color: rgba(14, 107, 176, 1);
		border-left: 1px solid rgba(2, 20, 33, 0.15);
	}
}
</style>
